﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Warehouse;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("check-stock")]
    public class CheckStockController : BaseController<CheckStock, CheckStockPagingDto>
    {
        private readonly IWarehouseGoodsService warehouseGoodsService;
        private readonly ICheckStockService checkStockService;

        public CheckStockController(
            IWarehouseGoodsService warehouseGoodsService,
            ICheckStockService checkStockService)
        {
            this.warehouseGoodsService = warehouseGoodsService;
            this.checkStockService = checkStockService;
        }

        protected override async Task<IQueryable<CheckStock>> FilterQueryPaging(DbContext db, IQueryable<CheckStock> query, CheckStockPagingDto dto)
        {
            if (!string.IsNullOrWhiteSpace(dto.receiptNo))
            {
                query = query.Where(x => x.receiptNo == dto.receiptNo || x.receiptNo.Contains(dto.receiptNo));
            }

            if (!string.IsNullOrWhiteSpace(dto.warehouseId))
            {
                query = query.Where(x => x.warehouseID == dto.warehouseId);
            }

            if (dto.Date != null)
            {
                var start = dto.Date.Value.Date.AddHours(-8);
                var end = start.AddDays(1);

                query = query.Where(x => x.addTime >= start && x.addTime < end);
            }
            await Task.CompletedTask;
            return query;
        }

        public override async Task<ResponseEntity<PagedResultDto<CheckStock>>> QueryPaging([FromBody] CheckStockPagingDto dto)
        {
            var res = await base.QueryPaging(dto);

            if (res?.Data?.Items?.Any() ?? false)
            {
                res.Data.Items = await this.checkStockService.AttachWarehouse(res.Data.Items.ToArray());
                res.Data.Items = await this.checkStockService.AttachGoodsInfo(res.Data.Items.ToArray());
            }

            return res;
        }

        [HttpPost("add-multiple"), Obsolete]
        public async Task<ResponseEntity<CheckStock[]>> AddMultiple([FromBody] CheckStock[] data)
        {
            data.Should().NotBeNullOrEmpty();
            var no = await this.NoGeneratorService.GenerateReceiptNoAsync("PD");
            foreach (var m in data)
            {
                this.EntityHelper.InitEntity(m);
                m.receiptNo = no;
                m.isAuditing = false;
            }

            await this.Repo.InsertManyAsync(data, autoSave: true);

            return new ResponseEntity<CheckStock[]>().SetData(data);
        }

        [HttpPost("save-multiple")]
        public async Task<ResponseEntity<CheckStock[]>> SaveMultiple([FromBody] ReceiptNoItemsDto<CheckStock> data)
        {
            var res = await this.Repo.SaveReceiptItems(this.HttpContext.RequestServices, data, "PD");
            return new ResponseEntity<CheckStock[]>().SetData(res.Items);
        }

        [HttpPost("query-by-receipt-no")]
        public async Task<ResponseEntity<CheckStock[]>> QueryByReceiptNo([FromBody] ReceiptNoDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
            var query = this.SingleStoreService.ApplyToQueryable(this.Repo);
            var data = await query.Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();

            if (data.Any())
            {
                data = await this.checkStockService.AttachWarehouse(data);
                data = await this.checkStockService.AttachGoodsInfo(data);
            }

            return new ResponseEntity<CheckStock[]>().SetData(data);
        }
    }
}
